<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表白墙</title>
</head>
<body>
    <div class="container">
        <h1>表白墙</h1>
        <p>输入后点击提交, 会将信息显示在墙上</p>
        <div class="row">
            <span>谁</span>
            <input type="text" class="edit">
        </div>
        <div class="row">
            <span>对谁</span>
            <input type="text" class="edit">
        </div>
        <div class="row">
            <span>说什么</span>
            <input type="text" class="edit">
        </div>
        <div class="row">
            <input type="button" value="提 交" id="submit">
        </div>

        <!-- 每次点击 "提交" 都在下面新增一个 .row , 里面就是放置用户输入的话 -->

    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
<!--        function load(){-->
<!--            $.ajax({-->
<!--                type:'GET',-->
<!--                url:'message',-->
<!--                success:function(data,status){-->
<!--                    let container=document.querySelector('.container');-->
<!--                    for(let message of contains){-->
<!--                        let row=document.createElement('div');-->
<!--                        row.className='row';-->
<!--                        row.innerHTML=message.from+'对'+message.to+'说：'+message.message;-->
<!--                        container.appendChild(row);-->
<!--                    }-->
<!--                }-->

<!--        });-->
<!--    }-->
<!--        load();   -->
            function load() {
            // 通过这个 load 函数, 从服务器上获取到消息, 并进行展示
            $.ajax({
                type: 'GET',
                url: 'message',
                success: function(data, status) {
                    // data 是响应的 body, 此时的响应可能只是一个字符串格式, 可以手动的进行一个解析, 按照 json 格式解析成对象
                    let container = document.querySelector('.container');
                    // let messages = JSON.parse(data);
                    let messages = data;
                    for (let message of messages) {
                        let row = document.createElement('div');
                        row.className = 'row';
                        row.innerHTML = message.from + '对' + message.to + '说: '+ message.message;
                        container.appendChild(row);
                    }
                }
            });
        }
        load();

        let submitButton = document.querySelector('#submit');
        submitButton.onclick = function() {
            // 1. 先获取到编辑框的内容
            let edits = document.querySelectorAll('.edit');
            let from = edits[0].value;
            let to = edits[1].value;
            let message = edits[2].value;
            console.log(from + ", " + to + ", " + message);
            if (from == '' || to == '' || message == '') {
                // 对用户输入做一个简单的校验. 验证一下当前是否是合法的提交. 
                return;
            }
            // 2. 根据内容, 构造 HTML 元素. (.row 里面包含用户输入的话)
            let row = document.createElement('div');
            row.className = 'row';
            row.innerHTML = from + '对' + to + '说: ' + message;
            // 3. 把这个新的元素添加到 DOM 树上
            let container = document.querySelector('.container');
            container.appendChild(row);
            // 4. 清空原来的输入框
            for (let i = 0; i < edits.length; i++) {
                edits[i].value = '';
            }
            // 5.构造一个http请求，把消息发给服务器保存
            $.ajax({
                type: "POST",
                url: "message",
                data: JSON.stringify({from: from, to: to, message: message}),
                contentType: "application/json; charset=utf-8",
                success: function(data, status) {
                    if (data.ok == 1) {
                        console.log('提交消息成功!');
                    } else {
                        console.log('提交消息失败!');
                    }
                }
            })
        }
    </script>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .container {
            width: 400px;
            margin: 0 auto;
        }

        h1 {
            text-align: center;
            padding: 20px 0;
        }

        p {
            text-align: center;
            color: #666;
            padding: 10px 0;
            font-size: 14px;
        }

        .row {
            height: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        span {
            width: 90px;
            font-size: 20px;
        }

        input {
            width: 310px;
            height: 40px;
        }

        #submit {
            width: 400px;
            color: white;
            background-color: orange;
            border: none;
            border-radius: 5px;
            font-size: 18px;
        }

        #submit:active {
            background-color: black;
        }

        .edit {
            font-size: 18px; 
            padding-left: 5px;
        }
    </style>
</body>
</html>